//
//  ZYHTooltipView.m
//  自定义弹出框
//
//  Created by mac on 15/12/24.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "ZYHTooltipView.h"

@interface ZYHTooltipView ()

@property (nonatomic, strong) UILabel *tooltipTextLabel;
@property (nonatomic) CGPoint arrowPoint;
@property (nonatomic, strong) UIGestureRecognizer *tapGestureRecogniser;

@end

@implementation ZYHTooltipView

- (instancetype)initWithSingleLineTargetView:(UIView *)targetView hostView:(UIView *)hostView tooltipText:(NSString *)tooltipText width:(CGFloat)width originOfX:(CGFloat)origin_x originOfY:(CGFloat)origin_y
{
    
    if (!(self = [super initWithFrame:CGRectZero])) {
        return nil;
    }
    
    self.backgroundColor = [UIColor clearColor];
    
    //弹出框外形
    CGFloat textHeight = 20;
    CGFloat space = 5;
    CGFloat arrowHeight = 14;         //箭头高
    CGRect tooltipFrame = CGRectZero;
    tooltipFrame.origin.x = origin_x;
    tooltipFrame.origin.y = origin_y - 2;
    tooltipFrame.size.width = width;
    tooltipFrame.size.height = arrowHeight + space + textHeight + space;
    self.frame = tooltipFrame;
    //剪头的顶点指向目标的中点
    CGPoint myPoint = CGPointZero;
    myPoint.x = origin_x + width/2;
    myPoint.y = CGRectGetMaxY(targetView.frame);
    self.arrowPoint = myPoint;
    
    //单行文本
    CGRect labelFrame = CGRectZero;
    labelFrame.origin.x = space;
    labelFrame.origin.y = 20;
    labelFrame.size.width = width - 10;
    labelFrame.size.height = textHeight;
    self.tooltipTextLabel = [[UILabel alloc] initWithFrame:labelFrame];
    //self.tooltipTextLabel.backgroundColor = [UIColor yellowColor];
    self.tooltipTextLabel.textColor = [UIColor whiteColor];
    self.tooltipTextLabel.text = tooltipText;
    self.tooltipTextLabel.textAlignment = NSTextAlignmentCenter;
    self.tooltipTextLabel.font = [UIFont systemFontOfSize:16.0f];

    NSLog(@"labelFrame:%@",NSStringFromCGRect(self.tooltipTextLabel.frame));
    NSLog(@"arrowX:%f  arrowY:%f",myPoint.x,myPoint.y);
    [self addSubview:self.tooltipTextLabel];
    [hostView addSubview:self];
    return self;
}



- (instancetype)initWithDoubleLineTargetView:(UIView *)targetView hostView:(UIView *)hostView tooltipText:(NSString *)tooltipText width:(CGFloat)width originOfX:(CGFloat)origin_x originOfY:(CGFloat)origin_y
{
    
    if (!(self = [super initWithFrame:CGRectZero])) {
        return nil;
    }
    
    self.backgroundColor = [UIColor clearColor];
    //弹出文本
    CGRect labelFrame = CGRectZero;
    self.tooltipTextLabel = [[UILabel alloc] initWithFrame:labelFrame];
    //self.tooltipTextLabel.backgroundColor = [UIColor yellowColor];
    self.tooltipTextLabel.textColor = [UIColor whiteColor];
    self.tooltipTextLabel.text = tooltipText;
    //self.tooltipTextLabel.textAlignment = NSTextAlignmentCenter;
    self.tooltipTextLabel.numberOfLines = 0;
    self.tooltipTextLabel.font = [UIFont systemFontOfSize:16.0f];
    //计算文本的高度
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:tooltipText attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16.0f]}];
    CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(width - 10, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
    labelFrame.size.height = ceil(rect.size.height);
    rect.origin.x = 5;
    rect.origin.y = 20;
    rect.size.width = width - 10;
    self.tooltipTextLabel.frame = rect;
    
    //弹出框外形
    CGRect tooltipFrame = targetView.frame;
    tooltipFrame.origin.x = origin_x;
    tooltipFrame.origin.y = origin_y - 2;
    tooltipFrame.size.width = width;
    tooltipFrame.size.height = self.tooltipTextLabel.frame.size.height + 10 + 14;
    self.frame = tooltipFrame;
    //剪头的顶点
    CGPoint myPoint = CGPointZero;
    myPoint.x = targetView.frame.origin.x + (targetView.frame.size.width / 2);
    myPoint.y = CGRectGetMaxY(targetView.frame);
    self.arrowPoint = myPoint;
    
    NSLog(@"labelFrame:%@",NSStringFromCGRect(self.tooltipTextLabel.frame));
    NSLog(@"arrowX:%f  arrowY:%f",myPoint.x,myPoint.y);
    [self addSubview:self.tooltipTextLabel];
    [hostView addSubview:self];
    
    self.alpha = 1.0;
    self.tapGestureRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    [self addGestureRecognizer:self.tapGestureRecogniser];
    
    
    
    return self;
}


- (void)handleTapGesture:(UIGestureRecognizer *)gestureRecogniser
{
    [UIView animateWithDuration:1.0 animations:^{
        self.alpha = 0;
    }];
}



-(void)drawRect:(CGRect)rect{
    
    NSLog(@"rect:%@",NSStringFromCGRect(rect));
    UIColor *backgroundColour = [Tools getColor:@"43bee0"];

    //1.获取图形上下文
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Variable Declarations
    CGPoint point = [self convertPoint:self.arrowPoint fromView:self.superview];
    point.y = 2;
    CGFloat angle = 0;
    CGFloat arrowHeight = 14;
    NSLog(@"pointX:%f  pointY:%f",point.x,point.y);
    
    // Group
    {
        //画矩形
        CGRect aFrame = CGRectMake(0, arrowHeight, CGRectGetWidth(rect) , CGRectGetHeight(rect) - arrowHeight);
        UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRoundedRect:aFrame  cornerRadius: 5];
        [backgroundColour setFill];
        [rectanglePath fill];
        
       
         //画三角形
         CGContextSaveGState(context);
         CGContextTranslateCTM(context, (point.x), (point.y));
         CGContextRotateCTM(context, -angle * M_PI / 180);
         
         UIBezierPath* bezierPath = UIBezierPath.bezierPath;
         [bezierPath moveToPoint: CGPointMake(0, 0)];
         [bezierPath addCurveToPoint: CGPointMake(-7, arrowHeight) controlPoint1: CGPointMake(-2, 0) controlPoint2: CGPointMake(-7, arrowHeight)];
         [bezierPath addLineToPoint: CGPointMake(7, arrowHeight)];
         [bezierPath addCurveToPoint: CGPointMake(0, 0) controlPoint1: CGPointMake(7, arrowHeight) controlPoint2: CGPointMake(2, 0)];
         [bezierPath closePath];
         
         [backgroundColour setFill];
         [bezierPath fill];
         
         CGContextRestoreGState(context);
         
        
    }

}




@end
